package com.roc.web.action.kymer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.Esmidinfo;
import com.roc.pojo.Estrnlog;
import com.roc.pojo.Operators;
import com.roc.pojo.TransReport;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettflagService;
import com.roc.util.Constance;
import com.roc.util.Utils;
import com.roc.util.constant.AuditingResultCodeType;
import com.roc.util.constant.AuditingType;
import com.roc.util.constant.Constant;
@Component
@Scope("prototype")
@Namespace("/kymer")
public class KYmerAction extends PaginationBaseAction{

    private Logger logger=LogManager.getLogger(KYmerAction.class);
    private KYmerSearchCondition searchCondition;
    private List<Esmidinfo> estrnList=new ArrayList<Esmidinfo>();
    private int flag=0;
    private String mid;
    private AuditingItem auditing;
	@Autowired
	IPagenitionService pagenitionService;
	@Autowired
	IMerchantServices merchantServices;
	@Autowired
	ISettflagService settflagService;
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/kymer/querykymer.jsp")
    })
	public String init(){
		return SUCCESS;
	}
	
	@Action(value = "kymersearch", results = { @Result(name = "success", location = "/pages/kymer/kymersearch.jsp")
    })
	public String kymersearch(){
		Operators loginOperators = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		try {
			Esmidinfo esm=	merchantServices.queryMerinfobyid(mid);
			
			Map auditingMap=new HashMap();
			auditingMap.put(AuditingResultCodeType.A0.getCode(),AuditingResultCodeType.A0.getName());
			auditingMap.put(AuditingResultCodeType.A1.getCode(),AuditingResultCodeType.A1.getName());
			auditingMap.put(AuditingResultCodeType.A2.getCode(),AuditingResultCodeType.A2.getName());
			auditingMap.put(AuditingResultCodeType.A3.getCode(),AuditingResultCodeType.A3.getName());
			auditingMap.put(AuditingResultCodeType.A4.getCode(),AuditingResultCodeType.A4.getName());
			getRequest().setAttribute("auditingMap",auditingMap);
			getRequest().setAttribute("esm", esm);
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return SUCCESS;
	}
	@Action(value = "querykymer", results = { @Result(name = "success", location = "/pages/kymer/querykymer.jsp")
    })
	public String querykymer(){
		Operators loginOperators = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		try {
			
			if(Constance.Null2EmptyTrim(searchCondition.getMinDate()).equals("")||Constance.Null2EmptyTrim(searchCondition.getMaxDate()).equals("")){
				getRequest().setAttribute("msg", "请选择时间");
				
				return SUCCESS;
				
			}
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				
			}
			dealWidth();
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, estrnList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return SUCCESS;
	}
	
	//解冻操作
	@Action(value = "kymerupdate", results = { @Result(name = "success", location = "/pages/kymer/querykymer.jsp")
             })
	public String kymerupdate() throws Exception{
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		//调用service层
		try { 
			   Esmidinfo esm=	merchantServices.queryMerinfobyid(mid);
				//编辑审核结果
				auditing.setAudit_code(auditing.getResult_code());
				auditing.setAudit_name(auditing.getResultCodeName());
				auditing.setObj_Audited(mid);
				auditing.setOperators(logingMerchant.getAccountNo());
				//auditing.setResult_code(auditing.getAudit_code());
				//auditing.setResult_detail(auditing.getResult_detail());
				
				//当界面选择同意并且和枚举中的同意code相同时进行冻结。
				String merstuts = auditing.getResult_code();
			   if(merstuts.equals("A4")){
				   esm.setMerStatus(merstuts);
				   esm.setFlag(2);
			    }else {
			    	esm.setMerStatus(merstuts);
			    }
				
				
				boolean b =	settflagService.KYApply(auditing, esm,merstuts);
				if(b){
					addActionMessage("风控等级变更失败");
					
				}else{
					addActionMessage("风控等级变更成功");
				}
					
					logger.info("------------------------------操作员【"+logingMerchant.getAccountNo()+"】更改风控级别【"+mid+"】成功！-----------------------------------");
				} catch (ServiceException e) {
					logger.info("------------------------------操作员【"+logingMerchant.getAccountNo()+"】更改风控级别【"+mid+"】失败！-----------------------------------");
					logger.error(e.getMessage(),e);
					return ERROR;
				}
				return querykymer();
	}
	private void dealWidth(){
		if(searchRsList.size()>0 && searchRsList!=null){
			
			Iterator iterator=searchRsList.iterator();
			while (iterator.hasNext()) {
				Object[] obj= (Object[])iterator.next();
				Esmidinfo temp=new Esmidinfo();
				temp.setAccNo(Utils.dealCardNo( Constance.Null2EmptyTrim(obj[2])));
				temp.setAmount(Long.valueOf(Constance.Null2EmptyTrim(obj[4])));
				temp.setMid(Constance.Null2EmptyTrim(obj[0]));
				temp.setMerName(Constance.Null2EmptyTrim(obj[1]));
				temp.setEtstermid(Constance.Null2EmptyTrim(obj[3]));
				temp.setEstrndate(Constance.Null2EmptyTrim(obj[5]));
				temp.setMerStatus(Constance.Null2EmptyTrim(obj[6]));
				
				estrnList.add(temp);
			}
		}
		//return null;
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_BIGESTRNHIS_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_BIGESTRNHIS_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return  Constance.PAGE_MAX_SIZE+"";
	}

	public KYmerSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(KYmerSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public List<Esmidinfo> getEstrnList() {
		return estrnList;
	}

	public void setEstrnList(List<Esmidinfo> estrnList) {
		this.estrnList = estrnList;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public AuditingItem getAuditing() {
		return auditing;
	}

	public void setAuditing(AuditingItem auditing) {
		this.auditing = auditing;
	}
	

}
